package com.dms.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {

    @Value("${swagger.enabled}")
    private boolean enabled;

    @Value("${swagger.title}")
    private String title;

    @Value("${swagger.description}")
    private String description;

    @Value("${swagger.version}")
    private String version;

    @Value("${swagger.base-package}")
    private String basePackage;

    @Value("${swagger.contact.name}")
    private String contactName;

    @Value("${swagger.contact.email}")
    private String contactEmail;

    @Value("${swagger.contact.url}")
    private String contactUrl;

    private static final List<ResponseMessage> globalResponses = Arrays.asList(
        new ResponseMessageBuilder()
            .code(200)
            .message("请求成功")
            .responseModel(new ModelRef("ApiResponse"))
            .build(),
        new ResponseMessageBuilder()
            .code(400)
            .message("请求参数错误")
            .responseModel(new ModelRef("ApiResponse"))
            .build(),
        new ResponseMessageBuilder()
            .code(401)
            .message("未授权")
            .responseModel(new ModelRef("ApiResponse"))
            .build(),
        new ResponseMessageBuilder()
            .code(403)
            .message("禁止访问")
            .responseModel(new ModelRef("ApiResponse"))
            .build(),
        new ResponseMessageBuilder()
            .code(404)
            .message("资源不存在")
            .responseModel(new ModelRef("ApiResponse"))
            .build(),
        new ResponseMessageBuilder()
            .code(500)
            .message("服务器内部错误")
            .responseModel(new ModelRef("ApiResponse"))
            .build()
    );

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(enabled)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any())
                .build()
                .globalResponseMessage(org.springframework.web.bind.annotation.RequestMethod.GET, globalResponses)
                .globalResponseMessage(org.springframework.web.bind.annotation.RequestMethod.POST, globalResponses)
                .globalResponseMessage(org.springframework.web.bind.annotation.RequestMethod.PUT, globalResponses)
                .globalResponseMessage(org.springframework.web.bind.annotation.RequestMethod.DELETE, globalResponses);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .version(version)
                .contact(new Contact(contactName, contactUrl, contactEmail))
                .build();
    }

    @Bean
    public Docket userApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("用户模块")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.dms.modules.user"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("商品模块")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.dms.modules.product"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket orderApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("订单模块")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.dms.modules.order"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket cartApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("购物车模块")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.dms.modules.cart"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket paymentApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("支付模块")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.dms.modules.payment"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket logisticsApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("物流模块")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.dms.modules.logistics"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket merchantApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("商家模块")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.dms.modules.merchant"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket adminApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("管理员模块")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.dms.modules.admin"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket c2cApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("C2C模块")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.dms.modules.c2c"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket shopApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("店铺模块")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.dms.modules.shop"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket systemApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("系统模块")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.dms.modules.system"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket homeApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("首页模块")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.dms.modules.home"))
                .paths(PathSelectors.any())
                .build();
    }
} 